@extends('layouts/app')
@section('content')
    <div class="weui-cells food-type-list" style="margin-top: 0;">
        {{--<div class="weui-cell">--}}
            {{--<div class="weui-cell__bd">--}}
                {{--<p>标题文字</p>--}}
            {{--</div>--}}
            {{--<div class="weui-cell__ft">--}}
                {{--<span class="f30 mr-10">编辑</span>--}}
                {{--<span class="f30">删除</span>--}}
            {{--</div>--}}
        {{--</div>--}}
    </div>
    {{--添加分类--}}
    <div id="addType" class="weui-popup__container popup-bottom">
        <div class="weui-popup__overlay"></div>
        <div class="weui-popup__modal">
            <div class="toolbar">
                <div class="toolbar-inner">
                    <a href="javascript:;" class="picker-button close-popup">关闭</a>
                    <h1 class="title">添加分类</h1>
                </div>
            </div>
            <div class="modal-content">
                <div class="weui-cells weui-cells_form">
                    <div class="weui-cell">
                        <div class="weui-cell__hd"><label class="weui-label">名称</label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" name="menu_name" type="text"  placeholder="分类名称">
                        </div>
                    </div>
                    <div class="weui-cell">
                        <div class="weui-cell__hd"><label class="weui-label">排序</label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" name="menu_sort" type="text"  placeholder="排序">
                        </div>
                    </div>
                    <div class="weui-cell">
                        <button class="weui-btn weui-btn_primary f30" onclick="addType('addType')">提交</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{--修改分类--}}
    <div id="editType" class="weui-popup__container popup-bottom">
        <input class="weui-input" id="id" name="id" type="hidden">
        <div class="weui-popup__overlay"></div>
        <div class="weui-popup__modal">
            <div class="toolbar">
                <div class="toolbar-inner">
                    <a href="javascript:;" class="picker-button close-popup">关闭</a>
                    <h1 class="title">修改分类</h1>
                </div>
            </div>
            <div class="modal-content">
                <div class="weui-cells weui-cells_form">
                    <div class="weui-cell">
                        <div class="weui-cell__hd"><label class="weui-label">名称</label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" id="menu_name" name="menu_name" type="text"  placeholder="分类名称">
                        </div>
                    </div>
                    <div class="weui-cell">
                        <div class="weui-cell__hd"><label class="weui-label">排序</label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" value="0" name="menu_sort" type="text"  placeholder="排序">
                        </div>
                    </div>
                    <div class="weui-cell">
                        <button class="weui-btn weui-btn_primary f30" onclick="addType('editType')">提交</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        var sid = getRequest().sid;


        // 获取菜品分类列表
        function getFoodType () {

            $.post("/api/shop_menu/all", {shop_id: sid}, function (res) {
                if (typeof res === 'string') {
                    res = JSON.parse(res);
                }
                console.log("res", res);
                var data = {};
                data['type'] = 'foodTypeList';
                data['element'] = '.food-type-list';
                data['data'] = res.data;

                A.init(data);

            });
        }

        getFoodType();


        // 获取一条分类的信息
        function getOneType (id) {
            $.post("/api/shop_menu/one", {shop_id: sid, id: id}, function (res) {
                if (typeof res === 'string') {
                    res = JSON.parse(res);
                }

                console.log("res", res);
                var name = res.data.menu_name,
                    id = res.data.id;

                $("#menu_name ").val(name);
                $("#id ").val(id);
                $("#editType").popup();
            });
        }



        // 点击添加按钮
        $("#addF").click(function () {
            // 打开添加面板
            $("#addType").popup();
        });
        // 编辑一个分类edit
        $('.food-type-list').on('click', '.edit', function () {
            var cid = $(this).attr('data-id');

            console.log('cid', cid);
            getOneType(cid);
        });
        // 删除一个分类
        $('.food-type-list').on('click', '.del', function () {
            var cid = $(this).attr('data-id');

            console.log('cid', cid);
            $.post("/api/shop_menu/del", {shop_id: sid, id: cid}, function (res) {
                if (typeof res === 'string') {
                    res = JSON.parse(res);
                }
                console.log("res", res);
                $.alert(res.info, function () {
                    getFoodType();
                });

            });
        });


        // 添加分类
        function addType (type) {
            var url = '';
            if ( type === 'addType') {
                url = '/api/shop_menu/add';
            } else if (type === 'editType') {
                url = '/api/shop_menu/edit';
                var id = $("#" + type + " input[name='id']").val();
            }
            var menu_name = $("#" + type + " input[name='menu_name']").val(),
                menu_sort = $("#" + type + " input[name='menu_sort']").val();

            if (!menu_name) {
                return $.toast("分类名称不能为空!", "text")
            }
            var data = {};
            data['shop_id'] = sid;
            data['id'] = id;
            data['menu_name'] = menu_name;
            data['menu_sort'] = menu_sort || 0;


            $.post(url, data, function (res) {
                if (typeof res === 'string') {
                    res = JSON.parse(res);
                }
                $.alert(res.info, function () {
                    if (res.code == 200) {
                        getFoodType();
                    }
                });
            });
        }

        // 关闭
//        $(".close-popup").click(function () {
//            $.closePopup();
//            location.reload();
//        });

    </script>
@endsection
@extends('layouts/footer')

